1//
2//  KNPersistantState.m
3//  Security
4//
5//  Created by J Osborne on 7/28/13.
6//
7//
8
9#import "KNPersistantState.h"
10
11@implementation KNPersistantState
12
13-(NSURL*)urlForStorage
14{
15	return [NSURL URLWithString:@"Preferences/com.apple.security.KCN.plist" relativeToURL:[[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]];
16}
17
18+(instancetype)loadFromStorage
19{
20	KNPersistantState *state = [[KNPersistantState alloc] init];
21    if (!state) {
22        return state;
23    }
24    
25    id plist = @{@"lastWritten": [NSDate distantPast]};
26
27    NSError *error = nil;
28    NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
29    if (!stateData) {
30        NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
31    } else {
32        NSPropertyListFormat format;
33        plist = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
34        
35        if (plist == nil) {
36            NSLog(@"Can't deserialize %@, e=%@", stateData, error);
37        }
38    }
39    
40    state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
41    state.lastWritten = plist[@"lastWritten"];
42	state.pendingApplicationReminderInterval = plist[@"pendingApplicationReminderInterval"];
43	state.debugLeftReason = plist[@"debugLeftReason"];
44	state.pendingApplicationReminder = plist[@"pendingApplicationReminder"];
45	state.applcationDate = plist[@"applcationDate"];
46	if (!state.applcationDate) {
47		state.applcationDate = [NSDate distantPast];
48	}
49	if (!state.pendingApplicationReminder) {
50		state.pendingApplicationReminder = [NSDate distantFuture];
51	}
52    if (!state.pendingApplicationReminderInterval || [state.pendingApplicationReminderInterval doubleValue] <= 0) {
53        state.pendingApplicationReminderInterval = [NSNumber numberWithUnsignedInt:60 * 60 * 24 * 2];
54    }
55    
56    return state;
57}
58
59-(void)writeToStorage
60{
61    NSMutableDictionary *plist = [@{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus],
62									@"lastWritten": [NSDate date],
63									@"applcationDate": self.applcationDate,
64									@"pendingApplicationReminder": self.pendingApplicationReminder,
65                            } mutableCopy];
66	if (self.debugLeftReason) {
67		plist[@"debugLeftReason"] = self.debugLeftReason;
68	}
69	if (self.pendingApplicationReminderInterval) {
70		plist[@"pendingApplicationReminderInterval"] = self.pendingApplicationReminderInterval;
71	}
72    NSLog(@"writeToStorage plist=%@", plist);
73	
74    NSError *error = nil;
75    NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:[plist copy] format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
76    if (!stateData) {
77        NSLog(@"Can't serialize %@: %@", plist, error);
78        return;
79    }
80    if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
81        NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);
82    }
83}
84
85
86@end
87